package com.agile.leetcode.middle.longestpalindrome;

/**
 * @Author KD
 * @Date 2021/2/5 14:42
 */
public class LongestPalindrome {
    public static void main(String[] args) {
        LongestPalindrome palindrome = new LongestPalindrome();
        System.out.println(palindrome.getLongestPalindrome_1("ac"));
    }

    public String getLongestPalindrome_1(String s) {
        if (s.length() == 1) return s;
        if (isPalindrome(s.toCharArray())) return s;
        // 暴力匹配
        // 纪录最大长度
        int maxLength = 0;
        int begin = 0;
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length - 1; i++) {
            for (int j = i + 1; j < charArray.length; j++) {
                char[] temp = new char[j - i + 1];
                System.arraycopy(charArray, i, temp, 0, j - i + 1);
                if (j - i + 1 > maxLength && isPalindrome(temp)) {
                    maxLength = j - i + 1;
                    begin = i;
                }
            }
        }
        if (maxLength == 0) {
            return s.substring(0, 1);
        } else {
            return s.substring(begin, begin + maxLength);
        }

    }

    public String getLongestPalindrome(String s) {
      /*  //
        if (isPalindrome(s)) return s;
        // 中心扩散算法
        char[] chars = s.toCharArray();
        // 返回最大长度
        int maxLength = 0;
        String palindrome = "";
        for (int i = 0; i < chars.length; i++) {
            // 定义接收器
            int j = i;
            int l = j - 1;
            int r = j + 1;
            if (l >= 0) {
                if (isPalindrome(String.valueOf(chars[l])+String.valueOf(chars[j]))&&maxLength<2) {
                    maxLength = 2;
                    palindrome = String.valueOf(chars[l])+String.valueOf(chars[j]);
                }
            }
            if (r < chars.length) {
                if (isPalindrome(String.valueOf(chars[j])+String.valueOf(chars[r]))&&maxLength<2) {
                    maxLength = 2;
                    palindrome = String.valueOf(chars[j])+String.valueOf(chars[r]);
                }
            }
            if (l < 0 || r > chars.length - 1) continue;
            String result = String.valueOf(chars[l]) + String.valueOf(chars[j]) + String.valueOf(chars[r]);
            while (l >= 0 && r < chars.length) {
                // 判断是否是回环数
                boolean b = isPalindrome(result);
                if (b) {
                    // 拿到长度
                    int length = result.length();
                    if (length > maxLength) {
                        maxLength = length;
                        palindrome = result;
                    }
                    r++;
                    l--;
                    if (r<chars.length&&l>=0){
                        result = chars[l] + result + chars[r];
                    }
                } else {
                    break;
                }

            }
        }
        return palindrome==""?s.substring(0,1):palindrome;*/
        return null;
    }

    public boolean isPalindrome(char[] charArray) {
        // 定义数组最后一位
        int j = charArray.length - 1;
        for (int i = 0; i < charArray.length; i++) {
            if (j > 0 && charArray[i] != charArray[j]) {
                return false;
            } else {
                j--;
            }
        }
        return true;
    }
}
